package com.cskaoyan.preparestatement;

import com.cskaoyan.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 创建日期: 2021/11/23 15:52
 *
 * @author ciggar
 */
public class PrepareStatementDemo {

    public static void main(String[] args) throws SQLException {

        // 获取连接
        Connection connection = JDBCUtils.getConnection();

        // 获取Statement对象
        // 获取PrepareStatement对象
        PreparedStatement preparedStatement = connection.prepareStatement("select * from user where id = ? and password = ?");

        preparedStatement.setInt(1,3);
        preparedStatement.setString(2,"upan");

        // 执行SQL语句
        ResultSet resultSet = preparedStatement.executeQuery();

        while (resultSet.next()) {

            String name = resultSet.getString("name");

            System.out.println("name:" + name);

        }

        // 关闭资源
        JDBCUtils.closeReources(resultSet,preparedStatement,connection);


    }
}
